﻿define(["jquery","bbcare","jqprint","echarts",
    "scprit/department/vm/assessmentScreening/temperamentAssessment.ui.js",
    "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.servers.js"
],function ($,$bb,$jqprint,echarts,$ui,$servers) {
    var event = {};
    event.init = function(obj){
        var userId = obj.userId,type = obj.type,answerId = obj.answerId;
        var hisParams = $servers.hisParams(userId,type,'1','');
        var hisFn = function(data){
            for (var i = 0; i < data.rows.length; i++) {
                var testTime = data.rows[i].createTime.substring(0,10);
                data.rows[i].testAge = $servers.age($servers.dateDiff(data.rows[i].birthday,testTime),data.rows[i].pregnancyWeek);
                data.rows[i].testTime = testTime;
            }
            $ui.historyInit({"id":"#temperament-history","data":data.rows});
            if(!answerId){
                answerId = $("#temperament-history>li").eq(0).attr("answerId");
                checkedFn(answerId);
                getInfo(userId,answerId,type);
            }else{
                checkedFn(answerId);
                getInfo(userId,answerId,type);
            }
            //历史记录列表点击事件：
            $("#temperament-history>li").click(function(){
                $(this).addClass("select").siblings().removeClass("select");
                answerId = $(this).attr("answerId");
                getInfo(userId,answerId,type);
            });
        };
        $servers.history(hisParams,hisFn);
    };
    //获取评估记录详情：
    var getInfo = function(userId,evaId,type){
        var reportParams = $servers.reportParams(userId,evaId);
        var reportFn = function(data){
            var testTime = data.data.createTime.substring(0,10);
            data.data.testAge = $servers.age($servers.dateDiff(data.data.birthday,testTime),data.data.pregnancyWeek);
            $ui.resultInfoInit({"id":"#basic-info","data":data.data});
            $ui.resultInfoInit({"id":"#print-basicInfo","data":data.data});
            if(data.data.result.indexOf("=") >= 0){
                var arr = ["activityLevelScore","rhythmicScore","dodgeScore","adaptiveScore","intensityReactionScore",
                    "moodScore","persistenceScore","attentionScatteredScore","reactionThresholdScore","min","max","type"];
                for(var i = 0;i<arr.length;i++){
                    if(data.data.result.indexOf(arr[i]) >= 0){
                        data.data.result = data.data.result.replace(arr[i],'"'+arr[i]+'"');
                    }
                }
                data.data.result = data.data.result.replace(/=/g,':');
                data.data.result = data.data.result.replace(/'/g,'"');
            }
            var result = JSON.parse(data.data.result);
            for(var item in result){
                if(item != "type"){
                    result[item] = result[item].toFixed(2);
                }
            }
            if(result.type.indexOf("'") >= 0){
                result.type = result.type.substring(1,result.type.length-1);
            }
            $ui.resultTableInit({"id":"#table-result","data":result});
            $ui.resultTableInit({"id":"#print-tableResult","data":result});
            getGuides(userId,type,evaId,result.type);
        };
        $servers.report(reportParams,reportFn);
    };
    //获取指导意见
    var getGuides = function (userId,exam,evaId,type){
        var params = $servers.getGuidesParamsTem(userId,exam,evaId,type);
        var guidesFn = function(data){
            if(data.data){
                for(var i = 0;i<data.data.list.length;i++){
                    var list = [];
                    var lists =  data.data.list[i].lists;
                    for(var k= 0;k<lists.length;k++){
                        list.push({"item":lists[k]});
                    }
                    data.data.list[i].lists = list;
                }
                $ui.guidesInit({"id":"#guides","data":data.data.list});
                $ui.guidesInit({"id":"#print-guides","data":data.data.list});
            }else{
                $("#guides,#print-guides").html("暂无指导建议");
            }
        };
        $servers.getGuides(params,guidesFn);
    };
    var checkedFn = function(answerId){
        $("#temperament-history").find("li").each(function(i){
            var $this=$(this);
            if($this.attr("answerId")==answerId){
                $(this).addClass("select").siblings().removeClass("select");
            }
        });
    };
    return event;
});